<?php
include_once dirname(__FILE__)."/../database.php";
include_once dirname(__FILE__)."/../constants.php";
include_once dirname(__FILE__)."/../safe.php";
include_once dirname(__FILE__)."/../../config.inc.php";

class Users
{
    public static function getUserById($id)
    {
        return null;
    }
    
    public static function getUserByName($name)
    {
        return null;
    }    

    public static function getUserByMail($mail)
    {
        return null;
    }
    
    public static function createUserId()
    {
        $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
        $db->connect();
        
        $res1 = $db->query("SELECT MAX(`ID`) FROM `users` ");
        $res2 = $db->fetch_array();
        $db->close();
        
        return $res2["MAX(`ID`)"] + 1;
    }
}

class User
{
    public function User()
    {
        
    }
    
    public static function exists($userName, $nickName, $mail)
    {
        $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
        $db->connect();
        
        $query = array("UserName" => $userName,
                       "NickName" => $nickName,
                       "Mail" => $mail);
        
        $res = $db->query_or_exists("users", $query);
        $db->close();
        return $res;
    }
    
    public static function login($userName, $password)
    {
        $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
        $db->connect();
        
        $password = cryptPassword($password); //Codifica la password prima di usarla
        $query = array("UserName" => $userName,
                       "Password" => $password);
        
        $valid = $db->query_or_exists("users", $query);
        
        if($valid)
        {
            $query = array("Status" => USER_ONLINE, "LastLogin" => date("y-m-d"));
            $db->query_update("users", $query);
            
            session_start();
            $res = $db->query("SELECT * FROM `users` WHERE `UserName` = '$userName' AND `Password` = '$password'");
            $userData = $db->fetch_array();
            $res = $db->query("SELECT * FROM `planets` WHERE `Owner` = '".$userData["Id"]."'");
            
            if($db->affected_rows > 0)
            {
                $_SESSION[SESSION_HAS_PLANETS] = true;
            }
            else
            {
                $_SESSION[SESSION_HAS_PLANETS] = false;
            }
            
            $_SESSION[SESSION_USER_DATA]= $userData;
        }
        
        
        $db->close();
        return $valid;
    }
    
    public static function logout()
    {
        session_start();
        $_SESSION = array();
        session_destroy();
    }

    public function register($userName, $nickName, $password, $mail, $race)
    {
        $id = Users::createUserId();
        $now = date("y-m-d");
        
        $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
        $db->connect();
        
        $query = array("ID" => $id,
                       "Race" => $race,
                       "UserName" => safeText($userName),
                       "NickName" => safeText($nickName),
                       "Password" => cryptPassword($password),
                       "Mail" => safeText($mail),
                       "Points" => 0,
                       "Rank" => 0,
                       "RegistrationDate" => $now,
                       "LastLogin" => $now,
                       "Status" => USER_NOT_ACTIVATED);
        
        $db->query_insert("users", $query);
        $db->close();
    }
    
    public function activate()
    {
        
    }
    
    public function delete()
    {
        
    }    
    
    public function ban()
    {
        
    }
    
    public function unban()
    {
        
    }
    
    public function isBanned()
    {
        
    }
}
?>
